package com.xuchen.zhenyang.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.controller.base.BaseController;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.Goods;
import com.xuchen.zhenyang.entity.ReturnGoods;
import com.xuchen.zhenyang.enums.StatusEnum;
import com.xuchen.zhenyang.service.GoodsService;
import com.xuchen.zhenyang.service.ReturnBaseService;
import com.xuchen.zhenyang.service.ReturnGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("returnGoods")
public class ReturnGoodsController extends BaseController {


    @Autowired
    ReturnGoodsService returnGoodsService;
    @Autowired
    ReturnBaseService returnBaseService;
    @Autowired
    GoodsService goodsService;

    @GetMapping("")
    String index(Integer returnBaseId, HttpServletRequest request) {
        request.setAttribute("returnBaseId",returnBaseId);
        List<Integer> goodsIds = getGoodsIdByReturnBaseId(returnBaseId);
        if (goodsIds.size() > 0) {
            request.setAttribute("goodsList", goodsService.selectBatchIds(goodsIds));
        }
        return "return/goods/return-goods-list";
    }

    @GetMapping("list")
    @ResponseBody
    Result list(Integer returnBaseId) {
        List<ReturnGoods> list = returnGoodsService.selectList(new EntityWrapper<ReturnGoods>().eq("return_base_id", returnBaseId));
        return Result.success(list);
    }

    @GetMapping("toAdd")
    String toAdd(Integer returnBaseId, HttpServletRequest request) {
        request.setAttribute("returnBaseId", returnBaseId);
        List<Integer> ids = getGoodsIdByReturnBaseId(returnBaseId);
        request.setAttribute("goodsList", goodsService.selectList(new EntityWrapper<Goods>()
                .eq("status", StatusEnum.NORMAL.getId())
                .notIn("goods_id", ids)
                .orderBy("goods_code")));//查询 未添加的、未失效的、商品属性的
        return "return/goods/return-goods-add";
    }

    @PostMapping("doAdd")
    @ResponseBody
    @RequestLog
    Result doAdd(ReturnGoods myEntity) {
        myEntity.setCreateUser(getSessionUserName());
        returnGoodsService.insert(myEntity);
        returnBaseService.updateTotalMoney(myEntity.getReturnBaseId());
        goodsService.addGoodsStock(myEntity.getGoodsId(), myEntity.getGoodsCount());
        return Result.success();
    }

    @GetMapping("toEdit")
    String toEdit(ReturnGoods myEntity, HttpServletRequest request) {
        myEntity = returnGoodsService.selectById(myEntity);
        List<Integer> ids = getGoodsIdByReturnBaseId(myEntity.getReturnBaseId());
        //编辑时查询 未添加、当前选中的、未失效、商品属性
        request.setAttribute("goodsList", goodsService.selectList(new EntityWrapper<Goods>()
                .eq("status", StatusEnum.NORMAL.getId())
                .notIn("goods_id", ids)
                .orNew()
                .eq("goods_id", myEntity.getGoodsId())
                .orderBy("goods_code")));
        request.setAttribute("myEntity", returnGoodsService.selectById(myEntity));
        return "return/goods/return-goods-edit";
    }

    @PostMapping("doEdit")
    @ResponseBody
    @RequestLog
    Result doEdit(ReturnGoods myEntity) {
        ReturnGoods originalReturnGoods = returnGoodsService.selectById(myEntity);
        returnGoodsService.updateById(myEntity);
        returnBaseService.updateTotalMoney(myEntity.getReturnBaseId());
        if (myEntity.getGoodsId().equals(originalReturnGoods.getGoodsId())) {
            if (!myEntity.getGoodsCount().equals(originalReturnGoods.getGoodsCount())) {
                goodsService.addGoodsStock(myEntity.getGoodsId()
                        , originalReturnGoods.getGoodsCount().subtract(myEntity.getGoodsCount()).multiply(new BigDecimal("-1")));
            }
        } else {
            goodsService.addGoodsStock(myEntity.getGoodsId(), myEntity.getGoodsCount());
            goodsService.addGoodsStock(originalReturnGoods.getGoodsId(), originalReturnGoods.getGoodsCount().multiply(new BigDecimal("-1")));
        }
        return Result.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @RequestLog
    Result delete(ReturnGoods myEntity) {
        myEntity = returnGoodsService.selectById(myEntity);
        returnGoodsService.deleteById(myEntity);
        returnBaseService.updateTotalMoney(myEntity.getReturnBaseId());
        goodsService.addGoodsStock(myEntity.getGoodsId(), myEntity.getGoodsCount().multiply(new BigDecimal("-1")));
        return Result.success();
    }

    private List<Integer> getGoodsIdByReturnBaseId(Integer returnBaseId) {
        List<Integer> list = new ArrayList<>();
        List<ReturnGoods> returnGoodsList = returnGoodsService.selectList(new EntityWrapper<ReturnGoods>().eq("return_base_id", returnBaseId));
        for (ReturnGoods returnGoods : returnGoodsList) {
            list.add(returnGoods.getGoodsId());
        }
        return list;
    }
}
